package client

import (
	"fmt"
	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/clients/config_client"
	"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/model"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

type NacosClient struct {
	Client config_client.IConfigClient
	NamingClient naming_client.INamingClient
}

// New Nacos Client
func NewNacos(option *Options) *NacosClient {
	clientConfig := constant.ClientConfig{
		TimeoutMs:      option.TimeoutMs,
		ListenInterval: option.ListenInterval,
		BeatInterval:   option.BeatInterval,
		LogDir:              "./logs",
		//NamespaceId: "bb7a555d-aae1-4e03-b6a8-a7ed45c69e41",
	}
	serverConfigs := []constant.ServerConfig{
		constant.ServerConfig{
			ContextPath: "/nacos",
			IpAddr:      option.Address,
			Port:        option.Port,
		},
	}
	// Create Config Center
	client, err := clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})
	if err != nil {
		panic(err)
	}
	// Create Register Center
	namingClient,err:=clients.CreateNamingClient(map[string]interface{}{
		"serverConfigs":serverConfigs,
		"clientConfig":clientConfig,
	})
	if err!=nil{
		panic(err)
	}
	return &NacosClient{Client: client,NamingClient: namingClient}
}

// Publish Config
func (c *NacosClient) Publish(dataId, group, content string) bool {
	ok, err := c.Client.PublishConfig(vo.ConfigParam{
		DataId:  dataId,
		Group:   group,
		Content: content,
	})
	if err != nil {
		return false
	}
	return ok
}

// Get Config
func (c *NacosClient) Get(dataId, group string) string {
	s, err := c.Client.GetConfig(vo.ConfigParam{
		DataId: dataId,
		Group:  group,
	})
	if err != nil {
		return ""
	}
	return s
}

// Delete Config
func (c *NacosClient) Delete(dataId, group string) bool {
	ok,err:=c.Client.DeleteConfig(vo.ConfigParam{
		DataId:   dataId,
		Group:    group,
	})
	if err!=nil{
		return false
	}
	return ok
}

// Listen Config
func (c *NacosClient) Listen(dataId, group string,OnChange func(namespace, group, dataId, data string)) error {
	return c.Client.ListenConfig(vo.ConfigParam{
		DataId:   dataId,
		Group:    group,
		OnChange: OnChange,
	})
}

// Register Service
func (c *NacosClient) Register(ip string,port uint64) bool {
	ok,err:=c.NamingClient.RegisterInstance(vo.RegisterInstanceParam{
		Ip:          "123.56.24.220",
		Port:        8848,
		ServiceName: "demo.go",
		Weight:      10,
		ClusterName: "zwt",
		Enable:      true,
		Healthy:     true,
		Ephemeral:   true,
	})
	if err!=nil{
		fmt.Println("注册失败:",err)
		return false
	}
	return ok
}

// Deregister Service
func (c *NacosClient) Deregister(ip string,port uint64) bool {
		ok ,err:=c.NamingClient.DeregisterInstance(vo.DeregisterInstanceParam{
			Ip:          ip,
			Port:        port,
			ServiceName: "demo.go",
			Cluster:     "a",
			Ephemeral:   true,
		})
		if err!=nil{
			return false
		}
		return ok
}

// Get Service
func (c *NacosClient) GetService(name string) (model.Service,error) {
	return c.NamingClient.GetService(vo.GetServiceParam{
		Clusters:    []string{"a"},
		ServiceName: name,
	})
}

// Select Health Service
func (c *NacosClient) SelectOneHealthyInstance() (*model.Instance,error) {
	return c.NamingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
			ServiceName: "demo.go",
			Clusters:    []string{"a"},
		})
}
